package com.tqjc.execute.task.entity;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.tqjc.execute.exception.TaskRunningException;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.utils.PlcOperator;
import com.tqjc.execute.utils.PublicResult;
import com.tqjc.execute.work.callback.TaskCallback;
import com.tqjc.system.core.annotation.QueryField;
import com.tqjc.system.core.model.QueryCondition;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @Description: 任务明细
 * @author: pzy
 * @date: 2023年05月15日 15:14
 */

@TableName(value = "task_apart")
@Data
@EqualsAndHashCode(callSuper=false)
@DS("baotou")
@Schema(description = "任务部分表")
@Slf4j
public class TaskApart extends Model implements InitializingBean, DisposableBean {



    /**
     * 主键
     */
    @TableId(type = IdType.AUTO)
    @Schema(description = "任务明细主键ID")
    private int id;
    /**
     * 所属任务id
     */
    @Schema(description = "所属上级任务id")
    @QueryField(queryColumn="task_id", condition= QueryCondition.eq)
    private Integer taskId;

    /**
     * 天车类型
     */
    @Schema(description = "天车编号")
    private String craneType;
    /**
     * 任务类型
     */
    @Schema(description = "任务类型")
    private String type;

    /**
     * 钢板id
     */
    @Schema(description = "钢板编号")
    private String steelNo;

    /**
     * 磁砣模式
     */
    @Schema(description = "磁砣模式")
    private Integer workMode;
    /**
     * 开始时间
     */
    @Schema(description = "开始时间")
    private Date beginTime;
    /**
     * 结束时间
     */
    @Schema(description = "结束时间")
    private Date endTime;

    /**
     * 上层Task
     */
    @TableField(exist = false)
    @Schema(description = "上层Task")
    private Task task;

    @TableField(exist = false)
    private TaskCallback callback;

    public void execute() throws Exception {
        System.out.println("执行任务...");
        if (callback != null) {
            callback.onPreExecute();
        }
        action();
        if (callback != null) {
            callback.onPostExecute();
        }
    }

    public void setCallback(TaskCallback callback) {
        this.callback = callback;
    }

    /**
     * create by: pzy
     * description: 执行任务
     * create time: 2023/5/22 10:06
     *
     * @return java.util.concurrent.Future<com.tqjc.execute.utils.PublicResult>
     * @param: []
     */
    public void action() throws Exception {
        //执行前置任务
        getCallback().onPreExecute();

        try {
            //log.info("{}天车{}步骤开始执行",getCraneType(),getType());
            customAction();
            //清楚Bean对象
            //log.info("{}天车{}步骤执行完成",getCraneType(),getType());
            Thread.sleep(1000);


        } catch (Exception e) {
            throw e;
        }

        //执行后置任务
        getCallback().onPostExecute();

    }


    public void customAction() throws Exception {
        //执行前置任务
        getCallback().onPreExecute();

        try {
            Thread.sleep(1000);
            //System.out.println("正在执行任务" + getType());

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //执行后置任务
        getCallback().onPostExecute();

    }


    /**
     * create by: pzy
     * description: 根据任务生成步骤
     * create time: 2023/5/26 11:19
     *
     * @return void
     * @param: []
     */
    public void setTaskApartByTask(Task task) {

        this.setTaskId(task.getId());
        this.setCraneType(task.getCraneType());
        this.setTask(task);

    }

    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }

//    public List<Roller> getRollerByXAndY


}